﻿using Sidvall.IO;
using Sidvall.Web.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace Sidvall.Plugins.Test
{
    class Program
    {
        #region Main

        static void Main(string[] args)
        {
            RegisterTypes();

            try
            {
                TestSendGridAsync().Wait();

                System.Console.WriteLine("Done.");
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(Sidvall.SystemContext.Current.SystemExceptionManager.GetMessage(ex));
            }
            System.Console.ReadLine();
        }

        #endregion

        #region GetServiceManagerContext

        public static string GetServiceManagerContext()
        {
            var context = new ServiceManagerContext()
            {
                Stores = new StoreCollection(),
            };
            context.Stores.Add("1", @"C:\System\Storage\BMI", null, null);
            return context.Serialize();
        }

        #endregion
        #region RegisterTypes

        private static void RegisterTypes()
        {
            // Framework
            var authorizationProvider = new Sidvall.Security.PublicAuthorizationProvider();
            Sidvall.SystemContext.Current = new Sidvall.SystemContext()
            {
                //AccountManager = new Sidvall.Security.AccountManager(new BMI.Security.AccountProvider()),
                AuthorizationManager = new Sidvall.Security.AuthorizationManager(authorizationProvider),
                AuthenticationManager = new Sidvall.Security.AuthenticationManager(new Sidvall.Security.AuthenticationProvider()),
                AuthenticationStrategyManager = new Sidvall.Security.AuthenticationStrategyManager(new Sidvall.Security.AuthenticationStrategy()),
                CacheManager = new Sidvall.Caching.CacheManager(new Sidvall.Caching.MemoryCache()),
                ClaimManager = new Sidvall.Security.ClaimManager(new Sidvall.Security.ClaimsPrincipalManager()),
                CodeManager = new Sidvall.Reflection.CodeManager(new Sidvall.Reflection.Code()),
                LauncherManager = new Sidvall.SystemEnvironment.LauncherManager(new Sidvall.SystemEnvironment.Windows.Launcher()),
                UserExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.UserExceptionProvider()),
                SystemExceptionManager = new Sidvall.ExceptionHandling.ExceptionManager(new Sidvall.ExceptionHandling.SystemExceptionProvider()),
                HttpClientFactory = new Sidvall.Net.HttpClientFactory(),
                LogManager = new Sidvall.Logging.LogManager(new Sidvall.Logging.LogCollection()),
                //SmsClientManager = new Sidvall.Net.Sms.SmsClientManager(new Sidvall.Net.Sms.SmsClient("")),
                SmtpClientManager = new Sidvall.Net.Mail.SmtpClientManager(new Sidvall.Net.Mail.SmtpClient()),
                SoapClientFactory = new Sidvall.Net.SoapClientFactory(),
                SqlManager = new Sidvall.Data.SqlManager(new Sidvall.Data.SqlServerProvider()),
                SystemStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem()),
                TransactionScopeFactory = new Sidvall.Data.TransactionScopeFactory(),
                TypeCache = new Sidvall.Reflection.TypeCache(),
                UserManager = new Sidvall.Security.UserManager(new Sidvall.Security.PublicAuthorizationProvider()),
                UserResponseStrategyManager = new Sidvall.Security.UserResponseStrategyManager(new Sidvall.Security.UserResponseStrategy()),
                UserStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem(Sidvall.IO.FolderType.LocalApplicationData)),

                // Client specific
                PrincipalContextManager = new Sidvall.Security.PrincipalContextManager(new Sidvall.Security.PrincipalContextProvider()),
                PrincipalManager = new Sidvall.Security.PrincipalManager(new Sidvall.Security.PrincipalProvider()),

                // Host specific
                CultureManager = new Sidvall.Globalization.CultureManager(new Sidvall.Globalization.CultureProvider()),
                ResourceManager = new Sidvall.Resources.ResourceManager(new Sidvall.Resources.ResourceProvider()),
                //SettingsManager = new Sidvall.Configuration.SettingsManager(new BMI.UI.Wpf.Configuration.ApplicationSettings()),
            };
            Sidvall.Office.SystemContext.Current = new Sidvall.Office.SystemContext()
            {
                ExcelManager = new Sidvall.Office.ExcelManager(new Sidvall.Office.Excel()),
            };
            Sidvall.Reporting.SystemContext.Current = new Sidvall.Reporting.SystemContext()
            {
                ReportManager = new Sidvall.Reporting.ReportManager(new Sidvall.Reporting.Report()),
            };
        }

        #endregion
        #region XmlEscape

        private static string XmlEscape(string unescaped)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode node = doc.CreateElement("root");
            node.InnerText = unescaped;
            return node.InnerXml;
        }

        #endregion
        #region TestAzureStorageAsync

        public static async Task TestAzureStorageAsync()
        {
            // Initialize
            var localPath = @"C:\Users\anders0763\Documents\IT-projekt 2008.pdf";
            var azurePath = @"logos\test.pdf";
            Sidvall.IO.AzureSystemContext.Current.StorageConnectionString = @"";
            var localStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem());
            var azureStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.AzureFileSystem());

            var items = await azureStorageManager.GetFilesAsync("contracts/companies", null, SearchOption.AllDirectories, false).ConfigureAwait(false);

            // Copy to Azure
            await Sidvall.IO.StorageGroupManager.CopyFileAsync(localStorageManager, localPath, azureStorageManager, azurePath, null, CancellationToken.None).ConfigureAwait(false);

            // Read from Azure
            azurePath = await Sidvall.IO.StorageGroupManager.CopyFileToTemporaryPathAsync(azureStorageManager, azurePath, localStorageManager, null, CancellationToken.None).ConfigureAwait(false);

            // Open file
            await Sidvall.SystemContext.Current.LauncherManager.LaunchFileAsync(azurePath).ConfigureAwait(false);
        }

        #endregion
        #region TestNetStorageAsync

        public static async Task TestNetStorageAsync()
        {
            // Initialize
            var localPath = @"C:\Users\anders0763\Documents\IT-projekt 2008.pdf";
            var netPath = @"logos\test.pdf";
            Sidvall.IO.NetSystemContext.Current.BaseAddress = @"https://localhost:44300/";
            Sidvall.IO.NetSystemContext.Current.StoreId = "1";
            var localStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.FileSystem());
            var netStorageManager = new Sidvall.IO.StorageManager(new Sidvall.IO.NetFileSystem());

            // Copy to storage
            await Sidvall.IO.StorageGroupManager.CopyFileAsync(localStorageManager, localPath, netStorageManager, netPath, null, CancellationToken.None).ConfigureAwait(false);

            // Read from storage
            netPath = await Sidvall.IO.StorageGroupManager.CopyFileToTemporaryPathAsync(netStorageManager, netPath, localStorageManager, null, CancellationToken.None).ConfigureAwait(false);

            // Open local file
            await Sidvall.SystemContext.Current.LauncherManager.LaunchFileAsync(netPath).ConfigureAwait(false);
        }

        #endregion
        #region TestSendGridAsync

        public static async Task TestSendGridAsync()
        {
            // Initialize
            Sidvall.Net.Mail.SendGridSystemContext.Current.ApiKey = "";

            // Send message
            var message = new Sidvall.Net.Mail.MailMessage()
            {
                FromAddress = "",
                RecipientAddresses = "",
                Subject = "SendGrid Test",
                Body = "<b>testing SendGrid</b>",
                IsBodyHtml = true,
            };
            message.Attachments.AddAttachmentFromString("This is content of attachment", "attachment1.json");
            message.Attachments.AddAttachmentFromFile(@"D:\a.pdf");
            var stream = await Sidvall.SystemContext.Current.SystemStorageManager.OpenFileAsync(@"D:\b.pdf", FileMode.Open, FileAccess.Read).ConfigureAwait(false);
            message.Attachments.AddAttachmentFromStream(stream, "b.pdf");
            var client = new Sidvall.Net.Mail.SendGridSmtpClient();
            await client.SendAsync(message).ConfigureAwait(false);
        }

        #endregion
    }
}
